﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class Customer : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                BindDDL();
            }
        }

        #region Private Method
        private void BindDDL()
        {
            WebPartBind.BindMasterDataDDL(this.ddlNumOfEmp, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerNumOfEmp"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlCustomerLevel, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerCustomerLevel"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlVocation, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerVocation"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerMasterType"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlComeFrom, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerComeFrom"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlStauts, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerStauts"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlKindOfCompany, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerKindOfCompany"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlVolumeOfBusiness, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerVolumeOfBusiness"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlPrincipal, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerPrincipal"].ToString()));
            WebPartBind.BindTrackingDDL(this.ddlTracking);
        }

        private void BindEditDDL()
        {
            WebPartBind.BindMasterDataDDL(this.ddlEditNumOfEmp, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerNumOfEmp"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditCustomerLevel, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerCustomerLevel"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditVocation, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerVocation"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerMasterType"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditComeFrom, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerComeFrom"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditStauts, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerStauts"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditKindOfCompany, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerKindOfCompany"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditVolumeOfBusiness, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerVolumeOfBusiness"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditPrincipal, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDCustomerPrincipal"].ToString()));
            WebPartBind.BindTrackingDDL(this.ddlEditTracking);
        }
        private void BindGridView()
        {
            CustomerService customerServiceObj = ServiceFactory.getCustomerService();
            OperationResult<List<tb_Customer>> result = customerServiceObj.GetAllCustomer(1, 25);

            if (result.operationSuccess)
            {
                gvCustomerList.DataSource = result.obj;
                gvCustomerList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            BindEditDDL();
            CustomerService customerServiceObj = ServiceFactory.getCustomerService();

            OperationResult<tb_Customer> resultEdit = customerServiceObj.GetCustomerById(id);
            if (resultEdit.operationSuccess == true)
            {
                txtEditAddress.Text = resultEdit.obj.Address;
                txtEditBankInformation.Text = resultEdit.obj.BankInformation;
                ddlEditComeFrom.SelectedValue = resultEdit.obj.ComeFrom.ToString();
                ddlEditCustomerLevel.SelectedValue = resultEdit.obj.CustomerLevel.ToString();
                txtEditDescription.Text = resultEdit.obj.Description;
                txtEditFax.Text = resultEdit.obj.Fax;
                ddlEditKindOfCompany.SelectedValue = resultEdit.obj.KindOfCompany.ToString();
                txtEditName.Text = resultEdit.obj.Name;
                ddlEditNumOfEmp.SelectedValue = resultEdit.obj.NumOfEmp.ToString();
                ddlEditPrincipal.SelectedValue = resultEdit.obj.Principal.ToString();
                ddlEditStauts.SelectedValue = resultEdit.obj.Stauts.ToString();
                txtEditTel1.Text = resultEdit.obj.Tel1;
                txtEditTel2.Text = resultEdit.obj.Tel2;
                ddlEditTracking.SelectedValue = resultEdit.obj.Tracking.ToString();
                ddlEditType.SelectedValue = resultEdit.obj.Type.ToString();
                txtEditURL.Text = resultEdit.obj.URL;
                ddlEditVocation.SelectedValue = resultEdit.obj.Vocation.ToString();
                lbEidtId.Text = id.ToString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CustomerService customerServiceObj = ServiceFactory.getCustomerService();

            tb_Customer instance = new tb_Customer();

            instance.Address = txtAddress.Text;
            instance.BankInformation = txtBankInformation.Text;
            instance.ComeFrom = Int32.Parse(ddlComeFrom.SelectedValue);
            instance.CreateBy = 1;
            instance.CreateDate = DateTime.Now;
            instance.CustomerLevel = Int32.Parse(ddlCustomerLevel.SelectedValue);
            instance.Description = txtDescription.Text;
            instance.Fax = txtFax.Text;
            instance.KindOfCompany = Int32.Parse(ddlKindOfCompany.SelectedValue);
            instance.Name = txtName.Text;
            instance.NumOfEmp = Int32.Parse(ddlNumOfEmp.SelectedValue);
            instance.Principal = Int32.Parse(ddlPrincipal.SelectedValue);
            instance.Stauts = Int32.Parse(ddlStauts.SelectedValue);
            instance.Tel1 = txtTel1.Text;
            instance.Tel2 = txtTel2.Text;
            instance.Tracking = Int32.Parse(ddlTracking.SelectedValue);
            instance.Type = Int32.Parse(ddlType.SelectedValue);
            instance.URL = txtURL.Text;
            instance.Vocation = Int32.Parse(ddlVocation.SelectedValue);
            instance.VolumeOfBusiness = Int32.Parse(ddlVolumeOfBusiness.SelectedValue);

            OperationResult<tb_Customer> result = customerServiceObj.CreateCustomer(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            CustomerService customerServiceObj = ServiceFactory.getCustomerService();

            tb_Customer entity = new tb_Customer();

            entity.Id = editId;

            entity.Address = txtEditAddress.Text;
            entity.BankInformation = txtEditBankInformation.Text;
            entity.ComeFrom = Int32.Parse(ddlEditComeFrom.SelectedValue);

            entity.CustomerLevel = Int32.Parse(ddlEditCustomerLevel.SelectedValue);
            entity.Description = txtEditDescription.Text;
            entity.Fax = txtEditFax.Text;
            entity.KindOfCompany = Int32.Parse(ddlEditKindOfCompany.SelectedValue);
            entity.Name = txtEditName.Text;
            entity.NumOfEmp = Int32.Parse(ddlEditNumOfEmp.SelectedValue);
            entity.Principal = Int32.Parse(ddlEditPrincipal.SelectedValue);
            entity.Stauts = Int32.Parse(ddlEditStauts.SelectedValue);
            entity.Tel1 = txtEditTel1.Text;
            entity.Tel2 = txtEditTel2.Text;
            entity.Tracking = Int32.Parse(ddlEditTracking.SelectedValue);
            entity.Type = Int32.Parse(ddlEditType.SelectedValue);
            entity.URL = txtEditURL.Text;
            entity.Vocation = Int32.Parse(ddlEditVocation.SelectedValue);
            entity.VolumeOfBusiness = Int32.Parse(ddlEditVolumeOfBusiness.SelectedValue);

            OperationResult<tb_Customer> resultEdit = customerServiceObj.ModifyCustomer(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            CustomerService customerServiceObj = ServiceFactory.getCustomerService();
            tb_Customer deleteEntity = new tb_Customer();
            deleteEntity.Id = deleteId;
            customerServiceObj.DeleteCustomer(deleteEntity);

            BindGridView();
        }

        protected void gvCustomerList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvCustomerList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomerList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion

    }
}
